<template>
    <div class="t-c" @click="startCountdown">
        {{ currentNumber === 0 || currentNumber === 60 ? '获取验证码' : `${currentNumber}s后获取` }}
    </div>
</template>
<script setup lang="ts">
import { defineProps, withDefaults, ref } from 'vue'
import { sentRegisterVerityCode, sentForgetVerityCode } from '@/service/login'

interface Props {
    phoneNumber: string
    editText?: string
}
const props = withDefaults(defineProps<Props>(), {
    phoneNumber: '',
    editText: ''
})
const pattern = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
const currentNumber = ref(60)
const countingDown = ref(false)
const getRegisterVerityCodeData = async () => {
    const result = await sentRegisterVerityCode({ telephone: props.phoneNumber })
    if (!result.data && result.msg === '手机号码已注册') {
        return false
    }
    return true
}
const getSentForgetVerityCode = async () => {
    const result = await sentForgetVerityCode({ telephone: props.phoneNumber })
    if (!result.data && result.msg === '手机号码系统中不存在') {
        return false
    }
    return true
}
const startCountdown = async () => {
    if (!pattern.test(props.phoneNumber)) {
        uni.showToast({
            icon: 'none',
            title: '手机号格式错误',
            duration: 2000
        })
        return
    }
    let result: Boolean = false
    if (props.editText === '修改') {
        result = await getSentForgetVerityCode()
        if (!result) {
            uni.showModal({
                title: '提示',
                showCancel: false,
                content: '账号不存在,请前往注册',
                success: (res) => {
                    if (res.confirm) {
                        uni.navigateTo({
                            url: '/mainPages/register/index'
                        })
                    }
                }
            })
        }
    }
    if (props.editText === '注册') {
        result = await getRegisterVerityCodeData()
        if (!result) {
            uni.showModal({
                title: '提示',
                showCancel: false,
                content: '账号已存在,请登录使用',
                success: (res) => {
                    if (res.confirm) {
                        uni.reLaunch({
                            url: '/pages/login/index'
                        })
                    }
                }
            })
        }
    }

    if (!countingDown.value && result) {
        countingDown.value = true
        // getRegisterVerityCodeData()
        const timer = setInterval(() => {
            // eslint-disable-next-line no-plusplus
            currentNumber.value--
            if (currentNumber.value === 0) {
                clearInterval(timer)
                currentNumber.value = 60
                countingDown.value = false
            }
        }, 1000)
    }
}
</script>
<style scoped lang="scss">
.t-c {
    width: 80px;
    background: #5677fc;
    color: #fff;
    font-size: 12px;
    // border-radius: 50rpx;
    height: 26px;
    text-align: center;
    line-height: 26px;
    border-radius: 15px;
}
</style>
